package camid.distribution.activeobject;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

import camid.distribution.broker.Requestor;
import camid.distribution.protocol.Endpoint;

public class ProxyBuilder<E> 
{
	private Class<E> iface;
	private InvocationHandler handler; 
	
	public ProxyBuilder(Class<E> iface, ObjectScheduler scheduler) {
		this.iface = iface;
		this.handler = new LocalInvocationHandler(scheduler);
	}

	public ProxyBuilder(Class<E> iface, String sessionId, String objectId, Class<?> entityClass, Requestor requestor,
			Endpoint endpoint) 
	{
		this.iface = iface;
		this.handler = new RemoteInvocationHandler(sessionId, objectId, entityClass, requestor, endpoint);
	}

	public E newProxy() 
	{	
		@SuppressWarnings("unchecked")
		E proxy = (E) Proxy.newProxyInstance(
			iface.getClassLoader(), 
			new Class[] { iface }, 
			handler);
		
		return proxy;
	}

}
